/**
 * 
 */
package com.epart.widget.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * BASE64 인코딩, 디코딩 유틸
 * 
 * @author cwsong
 * @version 2010. 10. 25.
 */
public class Base64Util {

	public Base64Util() {
	}

	/**
	 * Base64Encoding을 수행한다. binany in ascii out
	 * 
	 * @param encodeBytes
	 *            encoding할 byte array
	 * @return encoding 된 String
	 */
	public static String encode(String input) {

		byte[] encodeBytes = input.getBytes();
		BASE64Encoder base64Encoder = new BASE64Encoder();
		ByteArrayInputStream bin = new ByteArrayInputStream(encodeBytes);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[] buf = null;

		try {
			base64Encoder.encodeBuffer(bin, bout);
		} catch (Exception e) {
			e.printStackTrace();
		}
		buf = bout.toByteArray();
		return new String(buf).trim();
	}

	/**
	 * Base64Decoding 수행한다.
	 * 
	 * @param strDecode
	 * @return decoding 된 문자열
	 */
	public static String decode(String strDecode) {

		BASE64Decoder base64Decoder = new BASE64Decoder();
		ByteArrayInputStream bin = new ByteArrayInputStream(strDecode.getBytes());
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[] buf = null;

		try {
			base64Decoder.decodeBuffer(bin, bout);
		} catch (Exception e) {
			e.printStackTrace();
		}

		buf = bout.toByteArray();
		return new String(buf);
	}

}
